package cn.lyq.card.ui.share


import android.view.View
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.ShareImageInfo
import cn.lyq.card.data.src.Result
import cn.lyq.card.data.src.Status
import cn.lyq.card.utils.hasIndex
import cn.lyq.card.utils.loadUrl
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_share_list.*
import kotlinx.android.synthetic.main.layout_share_image.*
import kotlinx.android.synthetic.main.layout_title.*

class ShareList2Activity : BaseActivity() {

    private val mModel by lazy {
        getVM(ShareImgVM::class.java)
    }
    private var selectedIndex: Int = 0
    private val imgSrcList = mutableListOf<ShareImageInfo>()
    private val viewList by lazy {
        arrayOf(img1, img2, img3)
    }
    private val ids = arrayOf(R.id.s1, R.id.s2, R.id.s3)
    private val srcIndexList = mutableListOf<Int>()

    override fun getLayoutId(): Int {
        return R.layout.act_share_list_temp
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_invite_agent)
        tvTitleSplit.visibility = View.GONE
    }

    override fun initListener() {
        super.initListener()

        v1.setOnClickListener {
            if (selectedIndex == 0) return@setOnClickListener
            if (selectedIndex == 2) {
                motionLayout.setTransition(R.id.s3, R.id.s1) //orange to blue transition
                actionTurn(true)
            } else {
                motionLayout.setTransition(R.id.s2, R.id.s1) //orange to blue transition
                actionTurn(false)
            }
            motionLayout.transitionToEnd()
            selectedIndex = 0
        }
        v2.setOnClickListener {
            if (selectedIndex == 1) return@setOnClickListener

            if (selectedIndex == 2) {
                motionLayout.setTransition(R.id.s3, R.id.s2)  //red to orange transition
                actionTurn(false)
            } else {
                motionLayout.setTransition(R.id.s1, R.id.s2) //blue to orange transition
                actionTurn(true)
            }
            motionLayout.transitionToEnd()
            selectedIndex = 1
        }
        v3.setOnClickListener {
            if (selectedIndex == 2) return@setOnClickListener
            if (selectedIndex == 0) {
                motionLayout.setTransition(R.id.s1, R.id.s3)  //red to orange transition
                actionTurn(false)
            } else {
                motionLayout.setTransition(R.id.s2, R.id.s3) //blue to orange transition
                actionTurn(true)
            }
            motionLayout.transitionToEnd()
            selectedIndex = 2
        }

        tvConfirm.setClick {
            val info =
                imgSrcList.takeIf { it.hasIndex(srcIndexList[1]) }?.run { this[srcIndexList[1]] }
            info?.let {
                ShareImgActivity.start(this, info)
            }
        }
    }

    private fun getSrcIndex(isRight: Boolean): Int {
        val target = if (isRight) {
            srcIndexList[srcIndexList.lastIndex] + 1
        } else {
            srcIndexList[0] - 1
        }
        return when {
            target < 0 -> (imgSrcList.size - 1)
            target >= imgSrcList.size -> 0
            else -> target
        }
    }

    private fun actionTurn(isRight: Boolean) {
        var imgIndex = if (isRight) {
            selectedIndex - 1
        } else {
            selectedIndex + 1
        }
        if (imgIndex > 2) {
            imgIndex = 0
        }
        if (imgIndex < 0) {
            imgIndex = 2
        }
        val srcIndex = getSrcIndex(isRight)
        if (isRight) {
            srcIndexList.add(srcIndex)
            srcIndexList.removeAt(0)
        } else {
            srcIndexList.removeAt(2)
            srcIndexList.add(0, srcIndex)
        }
        val url = imgSrcList
            .takeIf { list -> list.hasIndex(srcIndex) }
            ?.run { this[srcIndex].image ?: "" } ?: ""
        viewList[imgIndex].loadUrl(url)
    }

    override fun initObserver() {
        super.initObserver()
        mModel.getData().observe(this, Observer {
            when (it?.status) {
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.toString())
                    dismissLoading()
                }
                Status.SUCCESS -> loadSuccess(it)
                Status.LOADING -> showLoading()
            }
        })
    }

    private fun loadSuccess(it: Result<MutableList<ShareImageInfo>>) {
        imgSrcList.clear()

        val temp = arrayOf(
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3334358022,898301436&fm=15&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=243245320,788481118&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2663878896,3531502612&fm=15&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3376319616,4272293906&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2078429900,3271633995&fm=15&gp=0.jpg"
        )
        val elements = it.requireData()
        elements.withIndex().forEach {
            it.value.image = temp[it.index.coerceAtMost(temp.size - 1)]
        }
        imgSrcList.addAll(elements)
        srcIndexList.add(0)
        srcIndexList.add(getSrcIndex(true))
        srcIndexList.add(0,imgSrcList.size - 1)
        viewList.withIndex().forEach {
            val url = imgSrcList
                .takeIf { list -> list.hasIndex(it.index) }
                ?.run { this[it.index].image ?: "" } ?: ""
            it.value.loadUrl(url)
        }
        dismissLoading()
    }
}